﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Jlob.OpenErpNet;
using Jlob.OpenErpNet.Entities;
using System.Linq;
using System.Collections.Generic;

namespace Jlob.OpenErpNet.Test
{
    [TestClass]
    public class ModelTest
    {
        private const string OpenErpServer = "http://192.168.73.128:8069";
        private const string OpenerpDataBase = "POLTEST";
        private const string OpenerpUser = "admin";
        private const string OpenerpPass = "admin";

        #region Employee
        
        [TestMethod]
        public void Test_Create_Employee_with_min_values()
        {
            Employee em = new Employee();
            em.Name = "Test";
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            openErpService.AddEntity<Employee>(em);
            em = openErpService.GetEntities<Employee>(e => e.Name.Equals("Test")).FirstOrDefault();
            Assert.IsNotNull(em);
        }

        [TestMethod]
        public void Test_Update_Employee()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            em = openErpService.GetEntities<Employee>(e => e.Name.Equals("Test")).FirstOrDefault();
            em.IdentificationNumber = "45685424K";
            openErpService.UpdateEntity<Employee>(em);
            em = openErpService.GetEntities<Employee>(e => e.IdentificationNumber.Equals("45685424K")).FirstOrDefault();
            Assert.IsNotNull(em);
        }

        [TestMethod]
        public void Test_Delete_Employee()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            em = openErpService.GetEntities<Employee>(e => e.Name.Equals("Test")).FirstOrDefault();
            openErpService.DeleteEntity<Employee>(em);
            em = openErpService.GetEntities<Employee>(e => e.Name.Equals("Test")).FirstOrDefault();
            Assert.IsNull(em);
        }

        [TestMethod]
        public void Test_Include_User_in_employee()
        {
            IEnumerable<Employee> employees;
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            employees = openErpService.GetEntities<Employee>(e => e.Name.Equals("Administrator")).Include(e => e.User);
            Assert.IsNotNull(employees.First().User);
        }

        [TestMethod()]
        public void Test_Include_List()
        {
            IEnumerable<Model> models;
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            models = openErpService.GetEntities<Model>(e => e.Name.Equals("Models")).Include(e => e.Fields);
            Model model = models.FirstOrDefault();
            Assert.IsTrue(model.Fields.Count>0);
        }

        [TestMethod]
        public void Test_Equals_Method()
        {
            IEnumerable<Model> Models;
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            Models = openErpService.GetEntities<Model>(e => e.Id.Equals("1"));
            Assert.IsNotNull(Models);
        }

        [TestMethod]
        public void Test_Equals_Operand()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            em = openErpService.GetEntities<Employee>(e => e.id == 1).FirstOrDefault();
            Assert.IsNotNull(em);
        }

        [TestMethod]
        public void Test_Nullable_values()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            em = openErpService.GetEntities<Employee>(e => e.id == 1).FirstOrDefault();
            Assert.IsNotNull(em);
            Assert.AreEqual(1, em.id);
            Assert.IsNull(em.Birthday);
        }

        [TestMethod]
        public void Test_String_endswith_method()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, OpenerpDataBase, OpenerpUser, OpenerpPass);
            em = openErpService.GetEntities<Employee>(e => e.Name.EndsWith("trator")).FirstOrDefault();
            Assert.IsNotNull(em);
            Assert.AreEqual(1, em.id);
        }

        [TestMethod]
        public void Test_String_contains_method()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, "POLTEST", "admin", "admin");
            em = openErpService.GetEntities<Employee>(e => e.Name.Contains("minis")).FirstOrDefault();
            Assert.IsNotNull(em);
            Assert.AreEqual(1, em.id);
        }
        #endregion
    }
}
